/**
 * 
 */
package com.yidu.labyrinth.utils;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;

import org.springframework.web.multipart.MultipartFile;

/**描述
 * @author licheng
 * @date 2019年6月25日下午2:43:59
 */
public class Tools {

	/**
	 * 生成20位的随机字符串
	 * @return
	 */
	public static String getRandom() {
		String randomStr="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmsm");
		Random rd=new Random();
		String str="";
		for(int i=0;i<4;i++) {
			int t=rd.nextInt(randomStr.length());
			char c=randomStr.charAt(t);
			str+=c;
		}
		Date date=new Date();
		str=sdf.format(date)+str;
		return str;
	}
	/**
	 * 生成随机纯数字字符串
	 * @return
	 */
	public static String getRandomNumber() {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmsm");

		String str="";
		int c=(int)(Math.random()*1000000);
		str+=c;
		Date date=new Date();
		str=sdf.format(date)+str;
		return str;
	} 
	/**
	 * 生成随机字符串
	 * @param size
	 * @return
	 */
	public static String getRandom(int size) {
		String randomStr="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
		Random rd=new Random();
		String str="";
		for(int i=0;i<size;i++) {
			int t=rd.nextInt(randomStr.length());
			char c=randomStr.charAt(t);
			str+=c;
		}
		return str;

	}
	/**
	 * 上传文件的工具类
	 * @param request
	 * @param name
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	public static String write(HttpServletRequest request,String name) throws IOException, ServletException {
		Part part=request.getPart(name);
		if(part.getSize()==0) {
			return "无照片";
		}
		//上传文件的名字
		String fileName=part.getSubmittedFileName();
		//取到upload文件夹地址
		String upload=request.getServletContext().getRealPath("upload");

		//从图片名字截取最后小数点以后的字符
		String hz=fileName.substring(fileName.lastIndexOf("."));
		//生成随机数名字+格式
		fileName=Tools.getRandom()+hz;

		part.write(upload+"/"+fileName);
		return "upload/"+fileName;
	}
	/**
	 * 	得到当前时间
	 * @return
	 */
	public static String getCurDateTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		return sdf.format(date);
	}
	/**
	 * 生成时间组成的编号
	 * @return
	 */
	public static String getCurDateTimeNum() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		Date date = new Date();
		return sdf.format(date);
	}
	/**
	 * 	得到当前时间
	 * @return
	 */
	public static String getCurDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		return sdf.format(date);
	}
	
	
	

	/**
	 * shpingMVC 图片上传
	 * @param photo
	 * @param session
	 * @return
	 */
	public static String springImg(MultipartFile photo , HttpSession session) {
		//得到服务器的upload文件夹的地址
		String path = session.getServletContext().getRealPath("/upload");
		//System.out.println(path);//D://tomcat/springMvcDemo/upload/
		//123.jpg
		String name = photo.getOriginalFilename();//得到源文件的名字
		String[] ts = name.split("\\.");
		//取一个32位的随机字符串
		String fileName = Tools.getRandom();
		File file = new File(path+"/"+fileName+"."+ts[ts.length-1]);
		try {
			photo.transferTo(file);//将文件保存到服务器
		} catch (IllegalStateException | IOException e) { 
			e.printStackTrace();
		}
		String str = "upload/"+fileName+"."+ts[ts.length-1];
		if(name.equals("") || name == null) {
			return "" ;
		}else {
			return str;
		}
	}
}
